package org.hive13.android.drunkdialer;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class DrunkTxt extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drunktxt2);
	}
	
	public void onPickVictimBtn(View view) {
		ArrayList<Victims> victims = new ArrayList<Victims>();
		Uri contacts = People.CONTENT_URI;
		Cursor c = managedQuery(contacts, new String[] {People._ID, People.NAME, People.NUMBER}, null, null, null);
		if(c.moveToFirst()) {
			int nameColumn = c.getColumnIndex(People.NAME);
			int numberColumn = c.getColumnIndex(People.NUMBER);
			do {
				Victims victim = new Victims();
				victim.victimName = c.getString(nameColumn);
				victim.victimNumber = c.getString(numberColumn);
				victims.add(victim);
			} while(c.moveToNext());
		}
		c.close();
		if(victims.size() > 0) {
			Random rand = new Random();
			Victims target = victims.get(rand.nextInt(victims.size()));
			Log.w("Selected target", "Target victim is " + target.victimName);
			PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, DrunkTxt.class), 0);
			SmsManager sms = SmsManager.getDefault();
			sms.sendTextMessage(target.victimNumber, null, "I can hax more drunkz", pi, null);
			TextView title = (TextView)findViewById(R.id.DrunkTextTitle);
			title.setText("I haz s3n7 t3xt0r.");
			Toast.makeText(this, "Bacon is tasty. " + target.victimName, Toast.LENGTH_SHORT).show(); 
		}
		
	}

}
